//
//  GPImageMessageCell.m
//  Keping
//
//  Created by 23 on 2017/12/7.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPImageMessageCell.h"

@interface GPImageMessageCell ()

/**image*/
@property(nonatomic,weak) UIImageView *imageContentImgView;

@end

@implementation GPImageMessageCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UIImageView *imageContentImgView = [[UIImageView alloc] init];
        imageContentImgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContent)];
        [imageContentImgView addGestureRecognizer:tap];
        [self.contentBackground addSubview:imageContentImgView];
        self.imageContentImgView = imageContentImgView;
        
        
        [imageContentImgView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.mas_equalTo(self.contentBackground.mas_left).offset(10);
            make.right.mas_equalTo(self.contentBackground.mas_right).offset(-10);
            make.top.mas_equalTo(self.contentBackground.mas_top).offset(10);
            make.bottom.mas_equalTo(self.contentBackground.mas_bottom).offset(-10);
            
        }];
        
    }
    return self;
}

- (void)setMessage:(GPMessageItem *)message
{
    //设置父类的值和约束
    [super setMessage:message];
    
    if (message.isSender) {
        [self.imageContentImgView sd_setImageWithURL:nil placeholderImage:message.image];
    }else{
        [self.imageContentImgView sd_setImageWithURL:[NSURL URLWithString:message.fileURLPath]];
    }
    
    
    [self.imageContentImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentBackground.mas_left).offset(10);
        make.right.mas_equalTo(self.contentBackground.mas_right).offset(-10);
        make.top.mas_equalTo(self.contentBackground.mas_top).offset(10);
        make.bottom.mas_equalTo(self.contentBackground.mas_bottom).offset(-10);
        make.size.mas_equalTo(CGSizeMake(150, 200));
    }];
    
    
    //更新
    [self layoutIfNeeded];
    
    //计算cell的高度
    CGFloat cellHeight = CGRectGetMaxY(self.contentBackground.frame) + 10;
    message.cellHeight = cellHeight;
}

- (void)tapContent
{
    if ([self.delegate respondsToSelector:@selector(messageCell:didContentClicked:)]) {
        [self.delegate messageCell:self didContentClicked:self.message];
    }
}


@end
